<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="50%">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="直播id" prop="exportId">
        <el-input :readonly="formType == 'detail'" v-model="formData.exportId" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="追踪ID，报bug带" prop="traceId">
        <el-input :readonly="formType == 'detail'" v-model="formData.traceId" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="观看人数" prop="newWatchUv">
        <el-input :readonly="formType == 'detail'" v-model="formData.newWatchUv" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="最大在线人数" prop="maxOnlineWatchUv">
        <el-input :readonly="formType == 'detail'" v-model="formData.maxOnlineWatchUv" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="曝光人数" prop="impressionUv">
        <el-input :readonly="formType == 'detail'" v-model="formData.impressionUv" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="平均观看时长（秒）" prop="averageWatchSecondsPerAudience">
        <el-input :readonly="formType == 'detail'" v-model="formData.averageWatchSecondsPerAudience" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="新增关注人数" prop="newFollowUv">
        <el-input :readonly="formType == 'detail'" v-model="formData.newFollowUv" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="新增粉丝团人数" prop="newFansClubUv">
        <el-input :readonly="formType == 'detail'" v-model="formData.newFansClubUv" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="评论人数" prop="commentUv">
        <el-input :readonly="formType == 'detail'" v-model="formData.commentUv" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="打赏人数" prop="rewardUv">
        <el-input :readonly="formType == 'detail'" v-model="formData.rewardUv" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="分享直播间人数" prop="sharingUv">
        <el-input :readonly="formType == 'detail'" v-model="formData.sharingUv" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="热度" prop="hotQuota">
        <el-input :readonly="formType == 'detail'" v-model="formData.hotQuota" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="直播大屏基础数据json" prop="liveDashboardData">
        <el-input :readonly="formType == 'detail'" v-model="formData.liveDashboardData" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="内容力数据json" prop="liveComparisonIndex">
        <el-input :readonly="formType == 'detail'" v-model="formData.liveComparisonIndex" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="电商数据概要数据json" prop="liveEcDataSummary">
        <el-input :readonly="formType == 'detail'" v-model="formData.liveEcDataSummary" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="电商转化力数据json" prop="liveEcConversionMetric">
        <el-input :readonly="formType == 'detail'" v-model="formData.liveEcConversionMetric" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="电商画像数据json" prop="liveEcProfile">
        <UploadFile :disabled="formType == 'detail'" v-model="formData.liveEcProfile" />
      </el-form-item>
      <el-form-item label="电商渠道分布json" prop="liveDistributionChannel">
        <el-input :readonly="formType == 'detail'" v-model="formData.liveDistributionChannel" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="电商商品数据json" prop="singleLiveEcSpuDataPageV2">
        <el-input :readonly="formType == 'detail'" v-model="formData.singleLiveEcSpuDataPageV2" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="直播持续时间" prop="liveDuration">
        <el-input :readonly="formType == 'detail'" v-model="formData.liveDuration" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="直播开始时间" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          :readonly="formType == 'detail'"
          type="date"
          value-format="x"
          placeholder="请选择"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button v-if="formType != 'detail'" @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { WeixinvideoLivedataApi, WeixinvideoLivedataVO } from '@/api/integration/weixinvideolivedata'

/** 微信视频号直播大屏数据 表单 */
defineOptions({ name: 'WeixinvideoLivedataForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  exportId: undefined,
  traceId: undefined,
  newWatchUv: undefined,
  maxOnlineWatchUv: undefined,
  impressionUv: undefined,
  averageWatchSecondsPerAudience: undefined,
  newFollowUv: undefined,
  newFansClubUv: undefined,
  commentUv: undefined,
  rewardUv: undefined,
  sharingUv: undefined,
  hotQuota: undefined,
  liveDashboardData: undefined,
  liveComparisonIndex: undefined,
  liveEcDataSummary: undefined,
  liveEcConversionMetric: undefined,
  liveEcProfile: undefined,
  liveDistributionChannel: undefined,
  singleLiveEcSpuDataPageV2: undefined,
  liveDuration: undefined,
  startTime: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await WeixinvideoLivedataApi.getWeixinvideoLivedata(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as WeixinvideoLivedataVO
    if (formType.value === 'create') {
      await WeixinvideoLivedataApi.createWeixinvideoLivedata(data)
      message.success(t('common.createSuccess'))
    } else {
      await WeixinvideoLivedataApi.updateWeixinvideoLivedata(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    exportId: undefined,
    traceId: undefined,
    newWatchUv: undefined,
    maxOnlineWatchUv: undefined,
    impressionUv: undefined,
    averageWatchSecondsPerAudience: undefined,
    newFollowUv: undefined,
    newFansClubUv: undefined,
    commentUv: undefined,
    rewardUv: undefined,
    sharingUv: undefined,
    hotQuota: undefined,
    liveDashboardData: undefined,
    liveComparisonIndex: undefined,
    liveEcDataSummary: undefined,
    liveEcConversionMetric: undefined,
    liveEcProfile: undefined,
    liveDistributionChannel: undefined,
    singleLiveEcSpuDataPageV2: undefined,
    liveDuration: undefined,
    startTime: undefined
  }
  formRef.value?.resetFields()
}
</script>